var app = getApp();

function requestQuestion(that,d1,d2,d3){
  wx.request({
    url: app.globalData.phpUrl + 'Question/api',
    method: "POST",
    header: { 'content-type': 'application/json' },
    data: { api_name: 'detail', openid: d1, questionnaire_id: d2, question_num:d3},
    success: function (data) {
      console.log('问卷id--' + d2, '第几个问题--' + d3);
      console.log('问题数据--');
      console.log(data.data.data)
      var t2 = 1 ;
      var o = data.data.data.options;
      var l = o.length;
      for(var s = 0; s < l;s ++){
        if (o[s].type != 1) {
          t2 = 2;
          break 
        }
      };
      var delayTime = 6;
      var liD = data.data.data;

      liD.type = { t1: data.data.data.type, t2: t2 };
      
      function multiple(){
        var lll = liD.options.length;
        var arry = [];
        if (liD.select_option != 0){
          for (var rr = 0; rr < lll; rr++) {
            if (liD.select_option.indexOf(liD.options[rr].id == -1)) {
              arry[rr] = false
            }else{
              arry[rr] = true
            }
          }
        }else{
          for (var rr = 0; rr < lll; rr++) {
              arry[rr] = false
          }
        }
        return arry
      }

      that.setData({
        question_num: d3,
        multiple: liD.type.t1 == 2 ? multiple() : '',
        lisData: liD,
        delay:false,
        delayTime: delayTime
      });

      if (that.data.lisData.select_option == '0') {
        that.setData({
          delay: true,
        });
        var tg = setInterval(function () {
          delayTime--;
          that.setData({
            delayTime: delayTime
          });
          if (delayTime == 0) {
            clearInterval(tg);
            that.setData({
              delay: false,
            });
          }
        }, 1000)
      } else {
        if (that.data.lisData.type.t1 == 5) {
          for (var jj = 0; jj < l; jj++) {
            if (o[jj].id == that.data.lisData.select_option) {
              that.setData({
                next_num: o[jj].next_question == '0' ? Number(that.data.question_num) + 1 : o[jj].next_question
              })
            }
          }
        }
      };
  
      wx.hideLoading();
    }
  });
  console.log('hhhhhhhh')
};

// pages/reply/reply.js
Page({
  data: {
    next: false,
    isFold: true,
    animation: {},
    lisData: '',
    moreTitle: '',
    next_num:2,
    pre_num: 0,
    question_num:1,
    delay:false,
    multiple:[],
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部'
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      cover: options.cover,
      title: options.title,
      question_id: parseInt(options.id),
      question_count: options.question_count,
      opid: options.opid
    })

    wx.showLoading({
      title: '加载问题中...',
    });

    requestQuestion(this, options.opid, parseInt(options.id), this.data.question_num);
  },

  radioChange: function (e) {
    var that = this;
    var iD = e.detail.value;
    var d = that.data.lisData.options;
    var dLendth = that.data.lisData.options.length;
    
    //如果是逻辑题   设置下一题
    if (that.data.lisData.type.t1 == 5){
      for (var uu = 0; uu < dLendth; uu++){
        if (iD == d[uu].id){
           that.setData({
             next_num: d[uu].next_question == '0' ? Number(that.data.question_num) + 1 : d[uu].next_question
           })
        };
      };
    };

    that.setData({
      ['lisData.options']:d,
      ['lisData.select_option']: iD,
    });
  },


  checkboxChange: function (e) {
    var that = this;
    var iD = e.detail.value;
    var d = that.data.lisData.options;
    var dLendth = that.data.lisData.options.length;
    
    var multiple = that.data.multiple;

    that.setData({
      ['lisData.options']: d,
      ['lisData.select_option']: iD,
    });

    if (that.data.lisData.select_option != 0) {
      var ld = that.data.lisData;
      for (var rr = 0; rr < dLendth; rr++) {
        if (ld.select_option.indexOf(ld.options[rr].id.toString()) == '-1') {
          multiple[rr] = false
        } else {
          multiple[rr] = true
        }
      }
    } else {
      for (var rr = 0; rr < dLendth; rr++) {
          multiple[rr] = false
      }
    };

    console.log(that.data.lisData)

    that.setData({
      multiple: multiple
    });
  },
  
  moreTtile:function(){
    var that = this;
    that.setData({
      isFold:false
    });
    var query = wx.createSelectorQuery();
    query.select('#moreContent').boundingClientRect();
    query.exec(function (res) {
      that.animation.bottom(- res[0].height).step({ duration: 300 });
      that.setData({
        animation: that.animation.export(),
      });
    })
  },

  defaultTtile: function () {
    var that = this;
    that.setData({
      isFold: true
    });
    that.animation.bottom('0rpx').step({ duration: 300 });
    that.setData({
      animation: that.animation.export(),
    });
  },

  nex:function(e){

    if (e.currentTarget.dataset.use) {
      return;
    };

    var that = this;
    var isFull = false;
    var question_id = '';
    var da = that.data.lisData;
    var le = that.data.lisData.options.length;

    switch(da.type.t1){
      case 1:
              if (da.select_option != '0') {
                isFull = true;
              };
              if (isFull) {
                wx.showLoading({
                  title: '下一题...',
                });
                console.log('下一题...')
                console.log(da.select_option, da.id)
                wx.request({
                  url: app.globalData.phpUrl + 'Question/api',
                  method: "POST",
                  data: { api_name: 'answer', openid: that.data.opid, question_id: da.id, content: da.select_option },
                  success: function (data) {
                    console.log('提交答案---');
                    console.log(data.data.msg);
                    if (data.data.code == 2) {
                      wx.redirectTo({
                        url: './finish/finish?id=1'
                      });
                      return
                    }
                    that.setData({
                      next_num: that.data.question_num + 1,
                      pre_num: that.data.question_num
                    })
                    requestQuestion(that, that.data.opid, that.data.question_id, that.data.next_num);
                  }
                })

              } else {
                wx.showToast({
                  title: '未选择选项!',
                  icon: 'none',
                  duration: 2000,
                })
              }
         break;
      
      case 2:
        if (da.select_option != '0') {
          isFull = true;
        };
        if (isFull) {
          wx.showLoading({
            title: '下一题...',
          });
          console.log('下一题...')
          console.log(da.select_option, da.id)
          wx.request({
            url: app.globalData.phpUrl + 'Question/api',
            method: "POST",
            data: { api_name: 'answer', openid: that.data.opid, question_id: da.id, content: da.select_option },
            success: function (data) {
              console.log('提交答案---');
              console.log(data.data.msg);
              if (data.data.code == 2) {
                wx.redirectTo({
                  url: './finish/finish?id=1'
                });
                return
              }
              that.setData({
                next_num: that.data.question_num + 1,
                pre_num: that.data.question_num
              })
              requestQuestion(that, that.data.opid, that.data.question_id, that.data.next_num);
            }
          })

        } else {
          wx.showToast({
            title: '未选择选项!',
            icon: 'none',
            duration: 2000,
          })
        }
        break;
      
 
 
      case 3:
              if (da.select_option != '0') {
                isFull = true;
              };
              if (isFull) {
                wx.showLoading({
                  title: '下一题...',
                });
                console.log('下一题...')
                console.log(da.select_option, da.id)
                wx.request({
                  url: app.globalData.phpUrl + 'Question/api',
                  method: "POST",
                  data: { api_name: 'answer', openid: that.data.opid, question_id: da.id, content: da.select_option },
                  success: function (data) {
                    console.log('提交答案---');
                    console.log(data.data.msg);
                    if (data.data.code == 2) {
                      wx.redirectTo({
                        url: './finish/finish?id=1'
                      });
                      return
                    }
                    that.setData({
                      next_num: that.data.question_num + 1,
                      pre_num: that.data.question_num
                    })
                    requestQuestion(that, that.data.opid, that.data.question_id, that.data.next_num);
                  }
                })

              } else {
                wx.showToast({
                  title: '未选择选项!',
                  icon: 'none',
                  duration: 2000,
                })
              }
        break;
      case 4:
              var region = that.data.region[0] + ' ' + that.data.region[1] + ' ' + that.data.region[2]
              wx.showLoading({
                title: '下一题...',
              });
              console.log('下一题...')
              wx.request({
                url: app.globalData.phpUrl + 'Question/api',
                method: "POST",
                data: { api_name: 'answer', openid: that.data.opid, question_id: da.id, content: region },
                success: function (data) {
                  console.log('提交答案---');
                  console.log(data.data.msg);
                  console.log(data.data)
                  if (data.data.code == 2) {
                    wx.redirectTo({
                      url: './finish/finish?money=' + data.data.data.money
                    });
                    return
                  };
                  that.setData({
                    next_num: that.data.question_num + 1,
                    pre_num: that.data.question_num
                  });
                  // requestQuestion(that, that.data.opid, that.data.question_id, that.data.next_num);
                }
              })

        break;
      case 5:
              if (da.select_option != '0') {
                isFull = true;
              };
              if (isFull) {
                wx.showLoading({
                  title: '下一题...',
                });
                console.log('下一题...')
                console.log(da.select_option, da.id)
                wx.request({
                  url: app.globalData.phpUrl + 'Question/api',
                  method: "POST",
                  data: { api_name: 'answer', openid: that.data.opid, question_id: da.id, content: da.select_option },
                  success: function (data) {
                    console.log('提交答案---');
                    console.log(data.data.msg);
                    if (data.data.code == 2) {
                      wx.redirectTo({
                        url: './finish/finish?id=1'
                      });
                      return
                    };
                    that.setData({
                      pre_num: that.data.question_num
                    });
                    requestQuestion(that, that.data.opid, that.data.question_id, that.data.next_num);
                  }
                })

              } else {
                wx.showToast({
                  title: '未选择选项!',
                  icon: 'none',
                  duration: 2000,
                })
              }
        break;
      case 6:
        break;
      default:
      // n 与 case 1 ~ case 4 不同时执行的代码
    }
  },

  pre: function (e) {
    var that = this;
    var preview = that.data.pre_num;
    if (preview == 0){
      wx.showToast({
        title: '没有上一题',
        icon: 'none',
        duration: 2000,
      })
      return
    };

    wx.showLoading({
      title: '上一题...',
    });

    requestQuestion(that, that.data.opid, that.data.question_id, preview);

    that.setData({
      question_num: that.data.question_num - 1,
      delay: false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this;
    var query = wx.createSelectorQuery();
    query.select('#titleTxt').boundingClientRect()
    query.exec(function (res) {
      if(res[0].width < 250){
        that.setData({
          isFold: false
        });
      }
    });

    that.animation = wx.createAnimation({
      duration: 1000,
      transformOrigin: "100% 0",
      timingFunction: "ease",
    });

    that.setData({
      animation: that.animation.export(),
    });

  },
  
  previewImage:function(t){
    var url = t.currentTarget.dataset.url
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },


  qc: function () {
    var that = this;
    wx.request({
      url: app.globalData.phpUrl + 'Question/api',
      method: "POST",
      data: { api_name: 'clear_answer', openid: that.data.opid, question_id: 36 },
      success: function (data) {
        console.log('11--已清除');
      }
    })
  },
})